package com.yygo.webapp.listener;

import com.yygo.Constants;
import com.yygo.jobs.GrabTicketJob;
import com.yygo.jobs.MsgJob;
import com.yygo.jobs.RuleJob;
import com.yygo.model.Feedback;
import com.yygo.model.GrabResponse;
import com.yygo.model.Greeting;
import com.yygo.model.LoanRequest;
import com.yygo.model.enums.FeedbackStatus;
import com.yygo.model.enums.FeedbackType;
import com.yygo.model.enums.Phase;
import com.yygo.model.enums.RequestStatus;
import com.yygo.service.LoanRequestManager;
import org.apache.activemq.ScheduledMessage;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.messaging.simp.SimpMessagingTemplate;

import javax.jms.*;
import java.util.Date;
import java.util.logging.Logger;

/**
 * Created by tiansha on 2015/10/24.
 */
public class JmsMessageListener implements MessageListener {

    private static Log log = LogFactory.getLog(JmsMessageListener.class);

    private RuleJob ruleJob;
    public void setRuleJob(RuleJob ruleJob) {
        this.ruleJob = ruleJob;
    }

    private MsgJob msgJob;
    public void setMsgJob(MsgJob msgJob) {
        this.msgJob = msgJob;
    }

    private GrabTicketJob grabTicketJob;
    public void setGrabTicketJob(GrabTicketJob grabTicketJob) {
        this.grabTicketJob = grabTicketJob;
    }

    private LoanRequestManager loanRequestManager;
    @Autowired
    public void setLoanRequestManager(LoanRequestManager loanRequestManager) {
        this.loanRequestManager = loanRequestManager;
    }

    @Autowired
    public SimpMessagingTemplate template;


    public void onMessage(Message message) {
        System.out.println("on message");
        LoanRequest loanRequest=null;
        try {
            Long reqId = message.getLongProperty("reqId");
            String type = message.getStringProperty("type");

            log.debug("onMessage: " + reqId + ", " + type);
            loanRequest = loanRequestManager.loadWholeLoanRequest(reqId);
            if (type.equals(Constants.MSG_TYPE_RULE)) {
                ruleJob.processLoanRequest(loanRequest);
            } else if (type.equals(Constants.MSG_TYPE_MESSAGE_CONFIRM_COVER)) {
                int times = message.getIntProperty("times");
                msgJob.sendConfirmCoverMessageToSlave(loanRequest, times);
            } else if (type.equals(Constants.MSG_TYPE_MESSAGE_QUALIFICATION_AUDIT)) {
                msgJob.sendQualificationAuditMessageToMaster(loanRequest);
            } else if (type.equals(Constants.MSG_TYPE_MESSAGE_UPLOAD_POLICY)) {
                msgJob.sendUploadPolicyMessageToMaster(loanRequest);
            } else if (type.equals(Constants.MSG_TYPE_AUTO_AUDIT)) {
                msgJob.autoAudit(loanRequest);
            } else if (type.equals(Constants.MSG_TYPE_GRAB_TICKET)) {
                String requestor = message.getStringProperty("requestor");
                String winner = "";
                if (loanRequest.getAuditUser() != null) {
                    winner = "";
                } else if (grabTicketJob.grabTicket(loanRequest, requestor)) {
                    winner = requestor;
                }
                GrabResponse grabResponse = new GrabResponse();
                grabResponse.setReqId(loanRequest.getId());
                grabResponse.setWinner(winner);
                //template.convertAndSend("/topic/grabTicket", grabResponse);
                template.convertAndSendToUser(requestor, "/grabTicket", grabResponse);
            }

        } catch (Exception ex) {
            try {
                String type = message.getStringProperty("type");
                if (type.equals(Constants.MSG_TYPE_RULE) && loanRequest != null) {
                    loanRequest.setPhase(Phase.dealer_submit);
                    loanRequest.setStatus(RequestStatus.dealer_submit);
                    loanRequest.setDealerComment("规则处理失败，请重新提交！");
                    loanRequestManager.save(loanRequest);
                }
            }catch (Exception e2) {}
            ex.printStackTrace();
        }
    }

}
